package com.lining.orangeservice.config;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.customizers.OpenApiCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SwaggerConfig {

    @Bean
    public OpenApiCustomizer openAPICustomizer() {
        return openAPI -> {
            // 添加安全方案
            SecurityScheme securityScheme = new SecurityScheme()
                    .type(SecurityScheme.Type.HTTP)
                    .scheme("bearer")
                    .bearerFormat("JWT");

            // 添加安全要求
            SecurityRequirement securityRequirement = new SecurityRequirement();
            securityRequirement.addList("Bearer Authentication");

            // 设置全局安全要求
            openAPI.getComponents().addSecuritySchemes("Bearer Authentication", securityScheme);
            openAPI.getPaths().values().forEach(pathItem -> {
                pathItem.readOperations().forEach(operation -> {
                    operation.addSecurityItem(securityRequirement);
                });
            });
        };
    }
}
